package app.qrcode.browser.ykpixabay.ui.login

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import androidx.navigation.Navigation
import app.qrcode.browser.ykpixabay.R
import cn.leancloud.LCUser
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_login.*

/**
 * Created by YiGo on 2022/4/21.
 */
class LoginFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_login, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val controller: NavController = Navigation.findNavController(
            this.requireActivity(),
            R.id.fragmentContainerView
        )
        Log.w("cdm", "LoginActivity.onCreate -> ${LCUser.getCurrentUser()}")
        login_btn.isEnabled = false
        edittext_login_password.addTextChangedListener(watcher)
        edittext_login_user_name.addTextChangedListener(watcher)
        loginProgressBar.visibility = View.INVISIBLE
        signup_btn.setOnClickListener {
            //跳转到注册界面
            controller.navigate(R.id.action_loginFragment_to_signupFragment)
        }
        login_btn.setOnClickListener {
            loginProgressBar.visibility = View.VISIBLE
            val name = edittext_login_user_name.text?.toString()?.trim()
            val pwd = edittext_login_password.text?.toString()?.trim()
            LCUser.logIn(name, pwd).subscribe(object : Observer<LCUser> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onNext(t: LCUser) {
                    Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show()
                    //跳转到主界面
                    controller.navigate(R.id.action_loginFragment_to_viewPicFragment)
                }

                override fun onError(e: Throwable) {
                    Toast.makeText(context, "${e.message}", Toast.LENGTH_SHORT).show()
                    loginProgressBar.visibility = View.INVISIBLE
                }

                override fun onComplete() {
                }

            })
        }
    }

    private val watcher = object : TextWatcher {
        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

        }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            val t1 = edittext_login_user_name.text.toString().isNotEmpty()
            val t2 = edittext_login_password.text.toString().isNotEmpty()
            login_btn.isEnabled = t1 and t2
        }

        override fun afterTextChanged(p0: Editable?) {

        }
    }
}